package com.chzc.fm.duotin.ui.mine;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.bean.mine.SubItemBean;
import com.chzc.fm.duotin.ui.mine.utils.MineUrl;
import com.chzc.fm.duotin.ui.other.BaseActivity;
import com.chzc.fm.duotin.utils.DuotinTask;
import com.chzc.fm.duotin.utils.GlobalParam;
import com.chzc.fm.duotin.utils.HttpUtil;
import com.chzc.fm.duotin.utils.ImageUtil;
import com.chzc.fm.duotin.utils.Unicode2CN;
import com.chzc.fm.duotin.widget.TitleBar;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class SubscribeActivity extends BaseActivity {

    private TextView textTip;
    private RecyclerView recyclerView;
    private TitleBar titleBar;

    private SubscribeAdapter adapter;

    private int page = 1;
    private int totalPage;


    private List<SubItemBean> subItems;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_subscribe;
    }

    @Override
    protected void initViews() {
        textTip = (TextView) findViewById(R.id.tv_sub_tip);
        recyclerView = (RecyclerView) findViewById(R.id.rv_sub_content);
        titleBar = (TitleBar) findViewById(R.id.title_bar);
    }

    @Override
    protected void initEvents() {
        textTip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(textTip.isSelected()){
                    textTip.setSelected(false);
                    adapter.setDel(false);
                }else{
                    textTip.setSelected(true);
                    adapter.setDel(true);
                }
            }
        });

        titleBar.setLeftListener(new TitleBar.mTitleBarListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void init() {
        titleBar.setTitleBarVisibility(true, "订阅");
        titleBar.setLeft(true, R.drawable.arrow_back_white);

        DuotinTask.IRequest request = new DuotinTask.IRequest() {
            @Override
            public Object doRequest() {
                return Unicode2CN.unicode2GBK((String) HttpUtil.doGet(MineUrl.getSubUrl(page)));
            }
        };

        DuotinTask.IRequestCallback callBack = new DuotinTask.IRequestCallback() {
            @Override
            public void success(Object object) {
                try {
                    JSONObject root = new JSONObject(object.toString());
                    String msg = root.getString(GlobalParam.ERROR_MSG);
                    if(GlobalParam.SUCCESS.equals(msg.toLowerCase())){
                        JSONObject data = root.getJSONObject("data");
                        JSONObject pageBean = data.getJSONObject("page");
                        JSONArray dataList = data.getJSONArray("data_list");
                        subItems = SubItemBean.arraySubItemBeanFromData(dataList.toString());
                        totalPage = pageBean.getInt("total_page");
                        if(page < totalPage){
                            page++;
                        }
                        setData();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void error(String msg) {

            }
        };

        DuotinTask task = new DuotinTask(request, callBack);
        putAsyncTask(task);

    }

    public void setData() {
        GridLayoutManager manager = new GridLayoutManager(this, 2);
        recyclerView.setLayoutManager(manager);

        adapter = new SubscribeAdapter(getBaseContext(), subItems);
        recyclerView.setAdapter(adapter);
    }


    private class SubscribeAdapter extends RecyclerView.Adapter<SubscribeAdapter.Holder>{
        private List<SubItemBean> subItemBeanList;
        private LayoutInflater inflater;

        private boolean isDel = false;

        public void setDel(boolean del) {
            isDel = del;
            notifyDataSetChanged();
        }

        public SubscribeAdapter(Context context, List<SubItemBean> subItemBeanList) {
            this.subItemBeanList = subItemBeanList;
            inflater = LayoutInflater.from(context);
        }

        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = inflater.inflate(R.layout.adapter_sub_item, null);
            return new Holder(view);
        }

        @Override
        public void onBindViewHolder(Holder holder, int position) {
            if(!isDel){
                holder.imageDel.setVisibility(View.GONE);
            }else{
                holder.imageDel.setVisibility(View.VISIBLE);
            }
            holder.textTitle.setText(subItemBeanList.get(position).getTitle());
            ImageLoader.getInstance().displayImage(subItemBeanList.get(position).getImage_url(), holder.imageAvatar, ImageUtil.getRoundOptions());
        }

        @Override
        public int getItemCount() {
            return subItemBeanList != null ? subItemBeanList.size() : 0;
        }

        protected class Holder extends RecyclerView.ViewHolder{
            public ImageView imageDel, imageAvatar;
            public TextView textTitle;
            public Holder(View itemView) {
                super(itemView);
                imageAvatar = (ImageView) itemView.findViewById(R.id.iv_sub_image);
                imageDel = (ImageView) itemView.findViewById(R.id.iv_sub_delete);
                textTitle = (TextView) itemView.findViewById(R.id.tv_sub_title);
            }
        }
    }
}
